#include <iostream>
#include <fstream>
#include "fun.h"
#include "logger.h"

using namespace cv;
using namespace std;

int printRatio(int total){
    static int count = 0;
    static int ratio = 0;
    count++;
    if(ratio != count * 10 / total){
        ratio = count * 10 / total;
        cout << ratio * 10 << "% "<<endl;
    }
    return 0;
}

int main(int argc, char *argv[]){
    if(argc < 7){
        cout << "illegal parameter!"<<endl;
        return -1;
    }
    PresetPara pPara;
    pPara.videoPath = argv[1];
    pPara.fps = atoi(argv[2]);
    pPara.duration = atoi(argv[3]);
    pPara.frame_h = atoi(argv[4]);
    pPara.frame_w = atoi(argv[5]);
    pPara.af = atoi(argv[6]);
    cout << "Processing..."<<endl;

    std::vector<std::string> videoList = readFile(pPara.videoPath);
    logger::record();
    logger::record()<< " =================== a new times have: "<< videoList.size()<<" videos ========================================= ";

    for(int i = 0; i < videoList.size(); i++){
        int re = printRatio(videoList.size());
        ifstream fin(videoList[i]);
        if(!fin){
            logger::record() << videoList[i] << "  file is not exist or not a video!";
            fin.close();
            return -1;
        }
        fin.close();

        VideoCapture cap(videoList[i]);
        int errCode = primaryFilter(videoList[i], cap, pPara);

        // 0 != errCode: video file have problem
        if(0 != errCode){
            continue;
        }

        // TODO: analysis video frame
        if (1 == pPara.af){
            errCode = frameAnalysis(videoList[i], cap);
        }
        cap.release();
    }
    logger::record()<< " =========================== end ================================================ ";
    return 0;
}

